<template>
    <el-dialog :title="`${ruleForm.view ? '查看' : ruleForm.id ? '编辑' : '新增'}港口`" :visible.sync="dialogVisible" width="30%" :before-close="handleClose">
        <el-form v-loading="loading" :model="ruleForm" :rules="rules" ref="ruleForm" label-width="auto">
            <c-form-template lable="">
                <el-col :span="24">
                    <el-form-item label="国家/地区" prop="country">
                        <el-select v-model="ruleForm.country" placeholder="请选择港口所在的国家/地区" @change="countryChange" style="display:block">
                            <el-option :label="item.label" :value="item.value" v-for="(item,index) in countryList" v-bind:key="index"></el-option>
                        </el-select>
                    </el-form-item>
                </el-col>
                <el-col :span="24">
                    <el-form-item label="所在城市" prop="city">
                        <el-select v-model="ruleForm.city" placeholder="请选择港口所在的城市" style="display:block">
                            <el-option :label="item.label" :value="item.value" v-for="(item,index) in cityList.children" v-bind:key="index"></el-option>
                        </el-select>
                    </el-form-item>
                </el-col>
                <el-col :span="24">
                    <el-form-item label="港口名称" prop="harbor">
                        <el-input v-model="ruleForm.harbor" placeholder="请输入港口名称"></el-input>
                    </el-form-item>
                </el-col>
            </c-form-template>
        </el-form>
        <span slot="footer">
            <el-button :loading="loading" @click="handleClose">取消</el-button>
            <el-button :loading="loading" type="primary" @click="handleSubmit">确认</el-button>
        </span>
    </el-dialog>
</template>
<script>
import countryList from '../../../..//components/form/countryData.js'
import cityList from '../../../../components/form/countryCityData.js'
export default {
    components: {},
    data () {
        return {
            dialogVisible: false,
            loading: false,
            countryList: countryList,
            cityList: [],
            ruleForm: {},
            rules: {
                country: [
                    { required: true, message: "请选择港口所在的国家/地区", trigger: "blur" },
                ],
                city: [
                    { required: true, message: "请选择港口所在的城市", trigger: "blur" },
                ],
                harbor: [
                    { required: true, message: "请输入港口名称", trigger: "blur" },
                ]
            },
        };
    },
    created () {
    },

    methods: {
        countryChange (e) {
            let Index = 0
            this.countryList.forEach((item, index) => {
                if (e == item.value) {
                    Index = index
                }
            })
            this.cityList = cityList[Index]
            this.ruleForm.addressCity = ""
        },
        handleOpen (item) {
            this.dialogVisible = true;
            if (item && item.id) {
                this.ruleForm = item
            }

        },
        handleClose () {
            this.ruleForm = {};
            this.$refs.ruleForm.resetFields();
            this.dialogVisible = false;
        },
        handleSubmit () {
            this.$refs.ruleForm.validate((valid) => {
                if (valid) {
                    this.$confirm("是否确认此操作?", "提示", {
                        confirmButtonText: "确定",
                        cancelButtonText: "取消",
                        type: "warning",
                    }).then(() => {
                        this.loading = true;
                        const data = JSON.parse(JSON.stringify(this.ruleForm));
                        this.$post({
                            url: `/ShopHarborInfo/${data.id ? "update" : "insert"}`,
                            data,
                        }).then(
                            () => {
                                this.$message.success("操作成功");
                                this.handleClose();
                                this.$parent.$parent.getList();
                                this.loading = false;
                            },
                            () => {
                                this.loading = false;
                            }
                        );
                    });
                }
            });
        }
    },
};
</script>
<style scoped lang="scss">
</style>
